package com.iraeta.service;

import cn.alibaba.open.param.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.iraeta.Config;
import com.iraeta.common.ServiceException;
import com.iraeta.entity.main.Srmmessage;
import com.iraeta.entity.main.type.SrmMessageStatus;
import com.iraeta.entity.main.type.SrmMessageType;
import com.iraeta.service.basic.SrmmessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProcessMessageTask {
    @Autowired
    SrmmessageService srmmessageService;
    @Autowired
    Config config;

    /*定时处理阿里的消息*/
    public void processSrmMessageLoop(){
        List<Srmmessage> srmmessageList = srmmessageService.ListCanProcessMsg();
        for (Srmmessage srmmessage : srmmessageList) {
            this.processSrmMessageOne(srmmessage);
        }
    }

    public void processSrmMessageOne(Srmmessage srmmessage){
        try {
            switch (srmmessage.getTypeid()){
                case SrmMessageType.caiGouOrder:
                    this.processCaiGouMsg(srmmessage);
                    break;
                case SrmMessageType.faHuoOrder:
                    this.processFaHuoMsg(srmmessage);
                    break;
                case SrmMessageType.shouHuoOrder:
                    this.processShouHuoMsg(srmmessage);
                    break;
                default:
                    throw new ServiceException("非法的消息类型");
            }
        }catch (Exception e){
            srmmessage.setErrormsg("未知异常:" + e.getMessage());
            srmmessage.setStatusid(SrmMessageStatus.fail);
            srmmessageService.update(srmmessage);
        }
    }

    private void processCaiGouMsg(Srmmessage srmmessage){
        if(srmmessage.getStatusid() != SrmMessageStatus.getDataSrm){
            ApiExecutor apiExecutor = new ApiExecutor(config.appKey,config.appSercret);
            AlibabaCaigouProcurementOrderQueryProcurementOrderDetailParam param = new AlibabaCaigouProcurementOrderQueryProcurementOrderDetailParam();
            param.setProcurementOrderId(srmmessage.getOrderid().longValue());
            AlibabaCaigouProcurementOrderQueryProcurementOrderDetailResult result = apiExecutor.execute(param, config.token).getResult();
            if(result == null){
                throw new RuntimeException("调用阿里接口未知错误");
            }
            if(!result.getSuccess()){
                throw new RuntimeException("调用阿里接口失败:" + result.getMessage());
            }
            AlibabaProcurementApiIntegrationModelIntegrationProcurementOrderModel data = result.getData();
            if(srmmessage.getStatusid() == SrmMessageStatus.unProcess || srmmessage.getStatusid() == SrmMessageStatus.fail ){   //如果第一次处理获取处理失败
                String orderData = JSON.toJSONString(data);
                srmmessage.setOrderdata(orderData);
                srmmessage.setStatusid(SrmMessageStatus.getDataSrm);
                srmmessage.setErrormsg("");
                srmmessageService.update(srmmessage);
            }
        }
        //todo data存储到erp
    }
    private void processFaHuoMsg(Srmmessage srmmessage){
        if(srmmessage.getStatusid() != SrmMessageStatus.getDataSrm){
            ApiExecutor apiExecutor = new ApiExecutor(config.appKey,config.appSercret);
            ComAlibabaProcurementBuyerSendGoodsServiceQuerySendGoodsNoteParam param = new ComAlibabaProcurementBuyerSendGoodsServiceQuerySendGoodsNoteParam();
            param.setId(srmmessage.getId());
            param.setNeedLogisticsInfo(false);
            ComAlibabaProcurementBuyerSendGoodsServiceQuerySendGoodsNoteResult result = apiExecutor.execute(param, config.token).getResult();
            if(result == null){
                throw new RuntimeException("调用阿里接口未知错误");
            }
            if(!result.getSuccess()){
                throw new RuntimeException("调用阿里接口失败:" + result.getMessage());
            }
            AlibabaProcurementApiSendgoodsIntegrationResultIntegrationSendGoodsNoteModel data = result.getData();
            if(srmmessage.getStatusid() == SrmMessageStatus.unProcess || srmmessage.getStatusid() == SrmMessageStatus.fail ){   //如果第一次处理获取处理失败
                String orderData = JSON.toJSONString(data);
                srmmessage.setOrderdata(orderData);
                srmmessage.setStatusid(SrmMessageStatus.getDataSrm);
                srmmessage.setErrormsg("");
                srmmessageService.update(srmmessage);
            }
        }
        //todo data存储到erp
    }
    private void processShouHuoMsg(Srmmessage srmmessage){
        if(srmmessage.getStatusid() != SrmMessageStatus.getDataSrm){
            ApiExecutor apiExecutor = new ApiExecutor(config.appKey,config.appSercret);
            ComAlibabaProcurementBuyerReceiveGoodsServiceQueryReceiveGoodsNoteParam param = new ComAlibabaProcurementBuyerReceiveGoodsServiceQueryReceiveGoodsNoteParam();
            param.setId(srmmessage.getId());
            ComAlibabaProcurementBuyerReceiveGoodsServiceQueryReceiveGoodsNoteResult result = apiExecutor.execute(param, config.token).getResult();
            if(result == null){
                throw new RuntimeException("调用阿里接口未知错误");
            }
            if(!result.getSuccess()){
                throw new RuntimeException("调用阿里接口失败:" + result.getMessage());
            }
            AlibabaProcurementApiReceivegoodsIntegrationResultIntegrationReceiveGoodsNoteModel data = result.getData();
            if(srmmessage.getStatusid() == SrmMessageStatus.unProcess || srmmessage.getStatusid() == SrmMessageStatus.fail ){   //如果第一次处理获取处理失败
                String orderData = JSON.toJSONString(data);
                srmmessage.setOrderdata(orderData);
                srmmessage.setStatusid(SrmMessageStatus.getDataSrm);
                srmmessage.setErrormsg("");
                srmmessageService.update(srmmessage);
            }
        }
        //todo data存储到erp
    }
}
